<?php
namespace app\admin\controller;

use think\Db;
class Volunteer extends Base {
    /***
     * 志愿者之星列表
     */
    public function index(){
        if(request()->isAjax()){
            $map['ua.assortment_type'] = 3;
            $map['uuu.user_attestation'] = 3;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('volunteer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->join('up_mechanism um','um.mechanism_id = uuu.user_to_mechanism_id','left')
                ->join('up_assortment ua','ua.assortment_id = uuu.user_title','left')
                ->count();//计算总页面
            $list = Db::name('volunteer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->join('up_mechanism um','um.mechanism_id = uuu.user_to_mechanism_id','left')
                ->join('up_assortment ua','ua.assortment_id = uuu.user_title','left')
                ->order('a.volunteer_sort desc')
                ->field('a.*,
                uuu.user_card,uuu.user_username,uuu.user_rel_name,uuu.user_phone,
                ua.assortment_name,um.mechanism_name')
                ->page($page, $limit)
                ->select();//计算总页面
            $total_time = '';
            foreach ($list as $key=>&$vo){
                $bir = substr($vo['user_card'], 6, 8);
                $year = (int) substr($bir, 0, 4);
                $month = (int) substr($bir, 4, 2);
                $day = (int) substr($bir, 6, 2);
                $vo['birthday'] = $year . "-" . $month . "-" . $day;
                $where['sign_time'] =array('>',0);
                $where['sign_end_time'] =array('>',0);
                $where['user_id'] =$vo['user_id'];

                $count = Db::table('up_institutional_activities_member')->where($where)->select();
                $vo['num'] = count($count);
                foreach ($count as $kk=>$vv){
                    //查询这个活动举办时长
                   $activity_time = Db::table('up_institutional_activities')->where('id',$vv['institutional_activities_id'])->value('activity_time');
                    $time = strtotime($vv['sign_end_time']) - strtotime($vv['sign_time']);
                    if(floor($time/3600) > $activity_time){//如果签到的时长大于活动的举办时长
                        $total_time += $activity_time;
                    }else{
                        $total_time += floor($time/3600);
                    }
                }
                $vo['time'] = $total_time?$total_time:0;
                $total_time ='';//活动总时长重新赋值
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /***
     * @return array
     * 更新排序
     */
    public function VolunteerSort(){
        Db::startTrans();// 启动事务
        $res = Db::table('up_volunteer')->where('volunteer_id',input('param.id'))->setField(input('param.field') , input('param.value'));

        if($res){
            Db::commit();// 提交事务
            writelog(session('uid'),session('username'),'排序更新成功',1);
            return ['code' => 200,'data' => '', 'msg' => '排序更新成功'];
        }else{
            Db::rollback ();//回滚事务
            writelog(session('uid'),session('username'),'排序更新失败',2);
            return ['code' => 100, 'data' => '', 'msg' => '排序更新失败'];
        }
    }

    /**
     * 撤销志愿者之星
     */
    public function update(){
        if(request()->isAjax()){
            $ids = input('param.ids');
            $user_id =Db::table('up_volunteer')->where('volunteer_id',$ids)->value('user_id');
            Db::startTrans();// 启动事务
            $res = Db::table('up_volunteer')->where('volunteer_id',$ids)->delete();
            $list= Db::table('up_u_user')->where('user_id',$user_id)->setField('is_zyx',1);

            if($res&&$list){
                Db::commit();// 提交事务
                writelog(session('uid'),session('username'),'撤销志愿者之星成功',1);
                return ['code' => 200,'data' => '', 'msg' => '撤销志愿者之星成功'];
            }else{
                Db::rollback ();//回滚事务
                writelog(session('uid'),session('username'),'撤销志愿者之星失败',2);
                return ['code' => 100, 'data' => '', 'msg' => '撤销志愿者之星失败'];
            }
        }
    }

    public function gain(){
        $id = input('param.id');
        $list =Db::table('up_volunteer')->where('volunteer_id',$id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
}